/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import { Injectable } from '@angular/core';
import { HttpClient} from '@angular/common/http';

import { environment } from '../../environments/environment'

import { Bingo } from '../domain/Bingo';
import { Prize } from '../domain/Prize';
import { Member } from '../domain/Member';
import { Winner } from '../domain/Winner';

import 'rxjs/add/operator/toPromise';

@Injectable()
export class RestService {
	
	constructor(private http: HttpClient) {}
	
	getCurrentBingo() {
		//console.log(environment);
		
		return this.http.get<any>(environment.server + "/rest/bingo/find/current")
            .toPromise()
			.then(res => <Bingo> res);
	}
	
	getPrizesByBingo(bingo:string) {

		bingo = encodeURIComponent(bingo);

		return this.http.get<any>(environment.server + "/rest/prize/listByBingo/" + bingo)
            .toPromise()
			.then(res => <Prize[]> res);
	}
	
	getAllMembers() {
		return this.http.get<any>(environment.server + "/rest/member/listAll")
			.toPromise()
			.then(res => <Member[]> res);
	}

    getMembersByBingo(bingo:string) {
        bingo = encodeURIComponent(bingo);

        return this.http.get<any>(environment.server + "/rest/member/listRemain/" + bingo)
            .toPromise()
            .then(res => <Member[]> res);
	}


    getWinnersByBingo(bingo:string) {

        bingo = encodeURIComponent(bingo);

        return this.http.get<any>(environment.server + "/rest/winner/listByBingo/" + bingo)
            .toPromise()
            .then(res => <Winner[]> res);
    }

    getWinnersByPrize(bingo:string, prize:number) {

        bingo = encodeURIComponent(bingo);

        return this.http.get<any>(environment.server + "/rest/winner/listByPrize/" + bingo + "/" + prize)
            .toPromise()
            .then(res => <Winner[]> res);
    }

    removeWinnerById(winnerId:string) {

		winnerId = encodeURIComponent(winnerId);

        return this.http.get<any>(environment.server + "/rest/winner/remove/" + winnerId)
            .toPromise()
            .then(res => res);
	}


    getRandomWinner(bingo:string, prize:number) {

        bingo = encodeURIComponent(bingo);

        return this.http.get<any>(environment.server + "/rest/winner/random/" + bingo + "/" + prize)
            .toPromise()
            .then(res => <Winner> res);
    }
	
}
